/*
   Copyright (c) 2023 李伟国
   shiro-learner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.service.impl;

import cn.platform.entity.UserRole;
import cn.platform.mapper.UserRoleMapper;
import cn.platform.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {
    @Override
    public Boolean deleteByUserIdRoleIds(Long userId, List<Long> roleIds) {
        int a = 0;
        for(Long roleId : roleIds){
            a += this.baseMapper.deleteByUserIdRoleId(userId, roleId);
        }
        return a == roleIds.size();
    }

    @Override
    public Boolean bindByUserIdRoleIds(Long userId, List<Long> roleIds) {
        int a = 0;
        for(Long roleId: roleIds){
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(userId);
            a+=this.baseMapper.insert(userRole);
        }
        return a == roleIds.size();
    }

    @Override
    public List<UserRole> findUserRoleByUserId(Long userId) {
        return this.baseMapper.selectByUserId(userId);
    }
}
